{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find N Unique Integers Sum up to Zero"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sumZero"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #和为零的 N 个不同整数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数&nbsp;<code>n</code>，请你返回 <strong>任意&nbsp;</strong>一个由 <code>n</code>&nbsp;个 <strong>各不相同&nbsp;</strong>的整数组成的数组，并且这 <code>n</code> 个数相加和为 <code>0</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 5\n",
    "<strong>输出：</strong>[-7,-1,1,3,4]\n",
    "<strong>解释：</strong>这些数组也是正确的 [-5,-1,1,2,3]，[-3,-1,2,-2,4]。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 3\n",
    "<strong>输出：</strong>[-1,0,1]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 1\n",
    "<strong>输出：</strong>[0]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-n-unique-integers-sum-up-to-zero](https://leetcode.cn/problems/find-n-unique-integers-sum-up-to-zero/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-n-unique-integers-sum-up-to-zero](https://leetcode.cn/problems/find-n-unique-integers-sum-up-to-zero/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5', '3', '1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n == 1: return [0]\n",
    "        if n%2==0: \n",
    "            l = list(range(-n//2,n//2+1))\n",
    "            return l[:n//2] + l[n//2+1:]\n",
    "        else:\n",
    "            return list(range(-n//2+1,n//2+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n%2==0:\n",
    "            for i in range(1,int(n/2+1)):\n",
    "                res.append(i)\n",
    "                res.append(-i)\n",
    "        else:\n",
    "            res.append(0)\n",
    "            for i in range(1,int((n-1)/2+1)):\n",
    "                res.append(i)\n",
    "                res.append(-i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        k = n//2\n",
    "        ans = list(range(-1, -k-1, -1)) + list(range(1, k+1, 1))\n",
    "        \n",
    "        if n % 2 != 0:\n",
    "            ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        x = [-i for i in range(n)]\n",
    "        x[0] = (n-1)*n//2\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        k = n//2\n",
    "        ans = list(range(-1, -k-1, -1)) + list(range(1, k+1, 1))\n",
    "        \n",
    "        if n % 2 != 0:\n",
    "            ans.append(0)\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 1:\n",
    "            ans = [i for i in range(-n // 2 + 1, n // 2 + 1)]\n",
    "        else:\n",
    "            ans = [i for i in range(-n // 2, 0)] + [i for i in range(1, n // 2 + 1)]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans=[i for i in range(1,n//2+1)]+[-i for i in range(1,n//2+1)]\n",
    "        if n%2==0:\n",
    "            return ans\n",
    "        else:\n",
    "            return ans+[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 1:\n",
    "            m = (n-1)/2\n",
    "            m = int(m)\n",
    "            re = list(range(-m,m+1))\n",
    "            return list(re)\n",
    "        else:\n",
    "            m = n/2\n",
    "            m = int(m)\n",
    "            re = list(range(-m,m))\n",
    "            re[-1] -= re[0]\n",
    "            return list(re)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 0:\n",
    "            return [i for i in range(1, n //2 + 1)] + [-j for j in range(1, n // 2 + 1)]\n",
    "        else:\n",
    "            return [i for i in range(1, n //2 + 1)] + [-j for j in range(1, n //2 +1)] + [0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n%2 == 0:\n",
    "            return [i for i in range(1, n//2+1)] + [-i for i in range(1, n//2+1)]\n",
    "        else:\n",
    "            return [i for i in range(1, n//2+1)] + [-i for i in range(1, n//2+1)] + [0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans=[]\n",
    "        for i in range(1,n//2+1):\n",
    "            ans.append(i)\n",
    "            ans.append(-i)\n",
    "        if n%2==1:\n",
    "            ans.append(0)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res=[]\n",
    "        if n%2==1:\n",
    "            res.append(0)\n",
    "        for i in range(1,n//2+1):\n",
    "            res.append(i)\n",
    "            res.append(-i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        i = n // 2\n",
    "        res = [j for j in range(-i,i+1)]\n",
    "        if n % 2 == 0:\n",
    "            res.remove(0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        i = n // 2\n",
    "        res = [j for j in range(-i,i+1)]\n",
    "        if n % 2 == 0:\n",
    "            res.remove(0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 1:\n",
    "            res.append(0)\n",
    "        for i in range(n//2):\n",
    "            res.append(i+1)\n",
    "            res.append(-i-1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        # 观察官方解法得到\n",
    "        if n == 2: \n",
    "            return [-1, 1]\n",
    "        else:\n",
    "            list1 = [i for i in range(n - 1)]\n",
    "            list1.append(-sum(list1))\n",
    "            return list1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ret = [0] * n\n",
    "        left = 0\n",
    "        right = n - 1\n",
    "        a = n // 2 + 1\n",
    "        while(left<right):\n",
    "            ret[left] = -a\n",
    "            ret[right] = a\n",
    "            a -= 1\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans =[]\n",
    "        if n%2 == 0:\n",
    "            for i in range(1,(n//2)+1):\n",
    "                ans.append(i)\n",
    "                ans.append(-i)\n",
    "        else:\n",
    "            for i in range(1,(n//2)+1):\n",
    "                ans.append(i)\n",
    "                ans.append(-i)\n",
    "            ans.append(0)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans = []\n",
    "        if n % 2 == 1:\n",
    "            ans.append(0)\n",
    "            for i in range(n//2):\n",
    "                ans.append(i+1)\n",
    "                ans.append(-(i+1))\n",
    "        else:\n",
    "            for i in range(n//2):\n",
    "                ans.append(i+1)\n",
    "                ans.append(-(i+1))\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        k=n//2\n",
    "        ans=list(range(-1,-k-1,-1))+list(range(1,k+1,1))\n",
    "        if n%2!=0:\n",
    "            ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n==1: \n",
    "            ans=[0] \n",
    "            return ans\n",
    "        if n==2:\n",
    "            ans = [1,-1]\n",
    "            return ans\n",
    "        ans = [x for x in range(n - 1)]\n",
    "        ans.append(-sum(ans))\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans = []\n",
    "        if n % 2 == 1:\n",
    "            for i in range(-(n // 2), n // 2 + 1):\n",
    "                ans.append(i)\n",
    "        else:\n",
    "            for i in range(-(n // 2), n // 2 + 1):\n",
    "                if i == 0:\n",
    "                    continue\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans=[]\n",
    "\n",
    "        for i in range(1,n//2+1):\n",
    "            ans.append(i)\n",
    "            ans.append(-i)\n",
    "\n",
    "        if n%2!=0:\n",
    "            ans.append(0)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans=[]\n",
    "        if n%2==0:\n",
    "            for i in range(1,int(n/2)+1):\n",
    "                ans.append(i)\n",
    "                ans.append(-i)   \n",
    "        else:\n",
    "            for i in range(1,int((n-1)/2)+1):\n",
    "                ans.append(i)\n",
    "                ans.append(-i)\n",
    "            ans.append(0)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n%2==0:\n",
    "            a=list(range(-(n//2),n//2+1))\n",
    "            a.remove(0)\n",
    "            return a\n",
    "        else:\n",
    "            return list(range(-(n//2),n//2+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res=[i for i in range(1,n)]            \n",
    "        res.append(-sum(res))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans = []\n",
    "        if n % 2 == 0 :\n",
    "            ans = list(range(1, (n // 2) + 1))\n",
    "            ans.extend(list(range(-(n // 2), 0)))   \n",
    "        else :\n",
    "            ans = list(range(1, ((n - 1) // 2) + 1))\n",
    "            ans.extend(list(range(-((n - 1) // 2), 0)))\n",
    "            ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n%2==1:\n",
    "            return list(range(-(n//2),n//2+1))\n",
    "        else:\n",
    "            return list(range(-n//2,0))+list(range(1,n//2+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n%2==0:\n",
    "            a=[]\n",
    "            for i in range(1,n//2+1):\n",
    "                a.append(i)\n",
    "                a.append(-i)\n",
    "            return a\n",
    "        else:\n",
    "            a=[0]\n",
    "            for i in range(1,n//2+1):\n",
    "                a.append(i)\n",
    "                a.append(-i)\n",
    "            return a  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2:\n",
    "            return [x for x in range(0-n//2,n//2+1)]\n",
    "        return [x for x in range(0-n//2,n//2+1) if x != 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 0:\n",
    "            cnt, mid, offset = 0, 0, 1\n",
    "            ans = []\n",
    "            while cnt < n:\n",
    "                ans.append(mid-offset)\n",
    "                cnt += 1\n",
    "                if cnt == n:\n",
    "                    break\n",
    "                ans.append(mid+offset)\n",
    "                offset += 1\n",
    "                cnt += 1\n",
    "        else:\n",
    "            cnt, mid, offset = 1, 0, 1\n",
    "            ans = [mid]\n",
    "            while cnt < n:\n",
    "                ans.append(mid-offset)\n",
    "                cnt += 1\n",
    "                if cnt == n:\n",
    "                    break\n",
    "                ans.append(mid+offset)\n",
    "                offset += 1\n",
    "                cnt += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n==1:\n",
    "            return [0]\n",
    "        t=[i for i in range(1,n)]\n",
    "        t.append(-sum(t))\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 1:\n",
    "            res.append(0)\n",
    "        for i in range(n // 2):\n",
    "            res.append(i + 1)\n",
    "            res.append(-i - 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        return list(range(int(n/2), 0, -1))+list(range(int((-n)/2), 0, 1)) + ([] if n%2==0 else [0])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        i = 0\n",
    "        ans = []\n",
    "        while i<n:\n",
    "            a = random.randint(-1000,1000)\n",
    "            if a not in ans:\n",
    "                ans += [a]\n",
    "                i += 1\n",
    "        b = sum(ans)\n",
    "        \n",
    "        while b != 0:\n",
    "            p = ans.pop(random.randint(0,n-1))\n",
    "            k = -(b-p)\n",
    "           \n",
    "            if k not in ans:\n",
    "                ans += [k]\n",
    "                b += k-p\n",
    "                \n",
    "            else:\n",
    "                ans += [p]\n",
    "            # print(b)\n",
    "            \n",
    "        \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        answer=[]\n",
    "        if n%2 == 0:\n",
    "            n2=int(n/2)\n",
    "            for i in range(1,n2+1):\n",
    "                answer.append(i)\n",
    "                answer.append(-i)\n",
    "        else:\n",
    "            n1=int((n-1)/2)\n",
    "            answer.append(0)\n",
    "            for i in range(1,n1+1):\n",
    "                answer.append(i)\n",
    "                answer.append(-i)\n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 0:\n",
    "            return [i for i in range(int(-n/2), 0)] + [j for j in range(1, int(n/2+1))]\n",
    "        else:\n",
    "            return [i for i in range(int(-n/2), int(n/2)+1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        # 挨个加相反数就可以确保和为 0。如果是偶数个，直接+i, + -i就可以\n",
    "        # 如果是奇数个，再额外加个 0\n",
    "        res=[]\n",
    "        if n%2==1:\n",
    "            res.append(0)\n",
    "        for i in range(1,n//2+1):\n",
    "            res.append(i)\n",
    "            res.append(-i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        return [x for x in range(-int(n / 2), int(n / 2) + 1) if n % 2 == 1 or x != 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        addArr = [1, -1]\n",
    "        ans = []\n",
    "        if n % 2 == 0:\n",
    "            for i in range(n // 2):\n",
    "                x = random.random()\n",
    "                ans.append(x)\n",
    "                ans.append(-x)\n",
    "        else:\n",
    "            for i in range(n // 2):\n",
    "                x = random.random()\n",
    "                ans.append(x)\n",
    "                ans.append(-x)\n",
    "            ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n%2==0:\n",
    "            ans=list(range(-(n//2),(n//2)+1))\n",
    "            ans.remove(0)\n",
    "            return ans\n",
    "        return list(range(-(n//2),(n//2)+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 0:\n",
    "            for i in range(1, n//2+1):\n",
    "                res.append(i)\n",
    "                res.append(-i)\n",
    "        elif n % 2 != 0:\n",
    "            res.append(0)\n",
    "            for i in range(1, n//2+1):\n",
    "                res.append(i)\n",
    "                res.append(-i)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(1, (n // 2)+1):\n",
    "            res.append(i)\n",
    "            res.append(-i)\n",
    "        if n % 2 == 1:\n",
    "            res.append(0)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(n >> 1):\n",
    "            res.extend((i+1, -i-1))\n",
    "        if n & 1:\n",
    "            res.append(0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "\n",
    "        num = int(n // 2)\n",
    "        if n % 2 == 0:\n",
    "            res = list(range(-num, 0)) + list(range(1, num+1))\n",
    "        else:\n",
    "            res = list(range(-num, num+1))\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        base = [i for i in range(1, n // 2 + 1)] + [-i for i in range(1, n // 2 + 1)]\n",
    "        if n % 2 == 0:\n",
    "            return base\n",
    "        else:\n",
    "            return base + [0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 1:\n",
    "            res.append(0)\n",
    "        n //= 2\n",
    "        for i in range(1, n + 1):\n",
    "            res.append(i)\n",
    "            res.append(-i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        k = n//2\n",
    "        ans = list(range(-1, -k-1, -1)) + list(range(1, k+1, 1))\n",
    "        \n",
    "        if n % 2 != 0:\n",
    "            ans.append(0)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        return [(i - n//2) for i in range(n)] if n % 2 else ([i for i in range(1, n//2 + 1)] + [-i for i in range(1, n//2 + 1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        list = []\n",
    "        if n %2 ==0:\n",
    "            for i in range(1,n//2+1):\n",
    "                list.append(i)\n",
    "                list.append(-i)\n",
    "        else:\n",
    "            list.append(0)\n",
    "            for i in range(1,(n-1)//2+1):\n",
    "                list.append(i)\n",
    "                list.append(-i)\n",
    "        return list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 1:\n",
    "            res.append(0)\n",
    "        for i in range(1, n//2 +1):\n",
    "            res.append(i)\n",
    "            res.append(-i)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        return [-i for i in range(1, n//2 + 1)] + [0] * (n%2) + [i for i in range(1, n//2 + 1)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        m=n//2\n",
    "        ans=[]\n",
    "\n",
    "        for i in range(1,m+1):\n",
    "            ans.append(i)\n",
    "            ans.append(-i)\n",
    "\n",
    "        if n%2==1:\n",
    "            ans.append(0)\n",
    "        \n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 0:\n",
    "            return [x for x in range(-int(n / 2), int(n / 2) + 1) if x != 0]\n",
    "        else:\n",
    "            return list(range(-int(n / 2), int(n / 2) + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n==1:\n",
    "            return [0]\n",
    "        res=[]\n",
    "        if n%2==0:\n",
    "            for i in range(int(n/2)):\n",
    "                res.append(i+1)\n",
    "                res.append(-(i+1))\n",
    "        else:\n",
    "            for i in range(int(n/2)):\n",
    "                res.append(i+1)\n",
    "                res.append(-(i+1))\n",
    "            res.append(0)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "\n",
    "        result = []\n",
    "\n",
    "        for i in range(1, n // 2 + 1):\n",
    "            result.append(i)\n",
    "            result.append(-i)\n",
    "\n",
    "        if n % 2 != 0:\n",
    "            result.append(0)\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans = []\n",
    "        for i in range(1, n // 2 + 1):\n",
    "            ans.append(i)\n",
    "            ans.append(-i)\n",
    "        if n % 2 == 1:\n",
    "            ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        n_2 = n // 2\n",
    "        if n == 1:\n",
    "            return [0]\n",
    "        elif n == 3:\n",
    "            return [-1, 0, 1]\n",
    "        elif n % 2 == 0:\n",
    "            # 可以整除时，正负对称\n",
    "            return [i for i in range(1, n_2 + 1)] + [-i for i in range(1, n_2 + 1)]\n",
    "        else:\n",
    "            # 不可整除时，正数为奇数，比负数多一个，此时无法确保和为0，\n",
    "            # 故负数最后一个为正数最后两个的和，此为观察样例得到\n",
    "            list1 = [i for i in range(1, (n_2 + 1 + 1))] + [-i for i in range(1, n_2)]\n",
    "            list1.append(-(list1[n_2] + list1[n_2 - 1]))\n",
    "            return list1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        sh,ys = divmod(n,2)\n",
    "        ret = []\n",
    "        for i in range(1,sh+1):\n",
    "            ret.append(i)\n",
    "            ret.append(-i)\n",
    "        if ys!=0:\n",
    "            ret.append(0)\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n%2==0:\n",
    "            return [i for i in range((-n+1)//2,n//2+1) if i!=0]\n",
    "        else:\n",
    "            return [i for i in range((-n+1)//2,n//2+1)]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res=[]\n",
    "        for i in range(1,n):\n",
    "            res.append(i)\n",
    "        res.append(-sum(res))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        return list(range(1 - n, n, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 1:\n",
    "            res.append(0)\n",
    "\n",
    "        for i in range(1, n//2 + 1):\n",
    "            res.append(i)\n",
    "            res.append(-i)\n",
    "            \n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ret=[]\n",
    "        if n%2!=0:\n",
    "            ret.append(0)\n",
    "        for i in range(1,n//2+1):\n",
    "            ret.append(i)\n",
    "            ret.append(-i)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        ans = [i for i in range(1, n)]\n",
    "        ans.append(sum(ans)*(-1))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n == 1:\n",
    "            return [0]\n",
    "        elif n == 3:\n",
    "            return [-1, 0, 1]\n",
    "        elif n % 2 == 0:\n",
    "            # 可以整除时，正负对称\n",
    "            return [i for i in range(1, n // 2 + 1)] + [-i for i in range(1, n // 2 + 1)]\n",
    "        else:\n",
    "            # 不可整除时，正数为奇数，比负数多一个，此时无法确保和为0，\n",
    "            # 故负数最后一个为正数最后两个的和，此为观察样例得到\n",
    "            list1 = [i for i in range(1, (n // 2 + 1 + 1))] + [-i for i in range(1, n // 2)]\n",
    "            list1.append(-(list1[n // 2] + list1[n // 2 - 1]))\n",
    "            return list1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n & 1:\n",
    "            return list(range((1 - n) // 2, (1 + n) // 2))\n",
    "        else:\n",
    "            return list(range(1 - n, n, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        return [i for i in range(-(n//2), n//2+1) if i != 0 or n % 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        if n % 2 == 1 :\n",
    "            return [ x - (n//2) for x in range(n)]\n",
    "        else :\n",
    "            return [ x + 1 for x in range(n//2)] + [ -x - 1 for x in range(n//2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        # temp=[0] if n % 2 ==1 else []\n",
    "        # for i in range(1,1+n//2):\n",
    "        #     temp+=[i]\n",
    "        #     temp+=[-i]\n",
    "        # return temp\n",
    "        if n==1: return [0]\n",
    "        temp=[i for i in range(1,n)]\n",
    "        temp+=[-sum(temp)]\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        a=[]\n",
    "        if n%2==0:\n",
    "            for i in range(-n//2,n//2+1):\n",
    "                if i!=0:\n",
    "                    a.append(i)\n",
    "        else:\n",
    "            for i in range(-(n+1)//2+1,(n+1)//2):\n",
    "                a.append(i)\n",
    "        return a\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(1,n//2+1):\n",
    "            res.append(i)\n",
    "            res.append(-1*i)\n",
    "        if n%2 == 1:\n",
    "            res.append(0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        li=[]\n",
    "        de=n//2\n",
    "        for i in range(1,de+1):\n",
    "            li.append(i)\n",
    "            li.append(-i)\n",
    "        if (n//2)*2==n:\n",
    "            li.sort()\n",
    "        else:\n",
    "            li.append(0)\n",
    "            li.sort()\n",
    "        return li\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumZero(self, n: int) -> List[int]:\n",
    "        res = []\n",
    "        if n % 2 == 0:\n",
    "            for i in range(1, (n // 2) + 1):\n",
    "                res.append(i)\n",
    "                res.append(-i)\n",
    "        else:\n",
    "            for i in range(1, (n // 2) + 1):\n",
    "                res.append(i)\n",
    "                res.append(-i)\n",
    "            res.append(0)            \n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
